/* 
Creates establishment level data sets with basic info (industry, microregion, totemployment by year, etc) 
		// input: workeryear_panel, microregions_municipality_concordance, contracts_cnes, rais_20102017
		// output: estabchars_wide, shdaysleave, shdaysleavefem
*/


cap log close
cap log using "$logs/estab_basic_info", replace 


/***************************************************************************
	Establishment characteristics
 ***************************************************************************/

/* Get estab-year level basic information */ 
use fakeid_estab year gender manager occ cnaesubcl_mode juridnature_mode estabidtype_mode municipality_mode ind_mode using "$files/rais/BR/workeryear_panel.dta", clear

//microregion
gen state_mode = floor(municipality_mode/1e4)
rename municipality_mode municipality
merge m:1 municipality using "$raw/microregions_municipality_concordance.dta", keepusing(microregion)
drop if _merge==2
drop _merge
rename municipality municipality_mode
rename microregion microregion_mode

//female managers
gen femmanager = (manager == 1 & gender == 2) 
gegen tot_managers = sum(manager), by(fakeid_estab year)
gegen fem_managers = sum(femmanager), by(fakeid_estab year)

//totals
gen n = 1
gegen tot_employment = sum(n), by(fakeid_estab year)
gen n_hasg = (gender!=.)
gegen tot_employment_hasg = sum(n_hasg), by(fakeid_estab year)
gen n_fem = (gender==2)
gegen tot_fememployment = sum(n_fem), by(fakeid_estab year)

* Unique establishment-year observations
keep 	fakeid_estab year ind_mode microregion_mode state_mode cnaesubcl_mode juridnature_mode estabidtype_mode ///
		tot_managers fem_managers tot_employment tot_employment_hasg tot_fememployment
duplicates drop 
unique fakeid_estab year
drop if year == . | fakeid_estab == . 

//share female managers
gen share_femmanager = fem_managers/tot_managers if tot_managers!=0 | tot_managers!=.
drop tot_managers fem_managers

* Wide version of the data
gen has_ = 1 
drop tot_employment_hasg
xtset fakeid_estab year 
reshape wide has_ share_femmanager tot_employment  tot_fememployment, i(fakeid_estab) j(year) 
forvalues y = 2011/2017 {
	replace tot_fememployment`y' = 0 if (tot_fememployment`y'==.)&(tot_employment`y'!=.)
}
save "$files/estabchars_wide_full.dta", replace


* Merge employer_id info from CNES
use "$files/unions/contracts_cnes.dta", clear	
keep employer_id 
duplicates drop 
//CNPJ
gen cnpj=subinstr(employer_id, ".", "", .)
replace cnpj=subinstr(cnpj, "/", "", .)
replace cnpj=subinstr(cnpj, "-", "", .)
//CEI (none are CEI in the data)
gen h = length(cnpj)
replace cnpj = "00"+cnpj if h==12
drop h 
isid cnpj 
tempfile links
save `links'

// merge estabchars_wide with employer_id 
use "$files/estabchars_wide_full.dta", clear 
tostring fakeid_estab, gen(cnpj) format("%14.0f")
gen a = length(cnpj) 
replace cnpj = "0"*(14-a)+cnpj
drop a
merge 1:m cnpj using `links', keepusing(employer_id) keep(1 3)

// keep establishment info for those signing CBAs
duplicates report fakeid_estab if  employer_id!=""
keep if employer_id!=""
isid fakeid_estab
isid employer_id
drop _merge
cap drop cnpj 

// state in string version
gen xxx = ""
replace xxx = "RO" if state_mode==11
replace xxx = "AC" if state_mode==12
replace xxx = "AM" if state_mode==13
replace xxx = "RR" if state_mode==14
replace xxx = "PA" if state_mode==15
replace xxx = "AP" if state_mode==16
replace xxx = "TO" if state_mode==17
replace xxx = "MA" if state_mode==21
replace xxx = "PI" if state_mode==22
replace xxx = "CE" if state_mode==23
replace xxx = "RN" if state_mode==24
replace xxx = "PB" if state_mode==25
replace xxx = "PE" if state_mode==26
replace xxx = "AL" if state_mode==27
replace xxx = "SE" if state_mode==28
replace xxx = "BA" if state_mode==29
replace xxx = "MG" if state_mode==31
replace xxx = "ES" if state_mode==32
replace xxx = "RJ" if state_mode==33
replace xxx = "SP" if state_mode==35
replace xxx = "PR" if state_mode==41
replace xxx = "SC" if state_mode==42
replace xxx = "RS" if state_mode==43
replace xxx = "MS" if state_mode==50
replace xxx = "MT" if state_mode==51
replace xxx = "GO" if state_mode==52
replace xxx = "DF" if state_mode==53
drop state_mode
rename xxx state_mode

* Save
compress
save "$files/estabchars_wide.dta", replace 


/***************************************************************************
	Share of leaves
 ***************************************************************************/

use "$files/rais/BR/rais_20102017.dta", clear
keep if year>=2012 & year<=2017
keep if emp1231==1
keep if admmonth==0

gegen workers = sum(emp1231), by(fakeid_estab year)
gen dayswork = 365*workers
gegen daysleave = sum(LOA_days), by(fakeid_estab year)
gen shdaysleave = daysleave/dayswork

forval i = 1/3 {
	gen start`i'_mdy = mdy(LOA_startmonth`i',LOA_startday`i',year)
	gen end`i'_mdy = mdy(LOA_endmonth`i',LOA_endday`i',year)
	gen LOA`i'_days = end`i'_mdy - start`i'_mdy + 1
}

forval i = 10(10)70 {
	gen LOA_days_`i' = 0
	replace LOA_days_`i' = LOA_days_`i' + LOA1_days if (LOA_cause1==`i')&(LOA1_days>0)&(LOA1_days<.) 
	replace LOA_days_`i' = LOA_days_`i' + LOA2_days if (LOA_cause2==`i')&(LOA2_days>0)&(LOA2_days<.) 
	replace LOA_days_`i' = LOA_days_`i' + LOA3_days if (LOA_cause3==`i')&(LOA3_days>0)&(LOA3_days<.) 
	replace LOA_days_`i' = 365 if (LOA_days_`i'>365)&(LOA_days_`i'<.)
}

forval i = 10(10)70 {
	gegen daysleave_`i' = sum(LOA_days_`i'), by(fakeid_estab year)
	gen shdaysleave_`i' = daysleave_`i'/dayswork
}

gegen xxx = tag(fakeid_estab year)
keep if xxx==1
keep fakeid_estab year shdaysleave*
xtset fakeid_estab year
compress
save "$files/shdaysleave.dta", replace
 
/***************************************************************************
	Share of leaves among women
 ***************************************************************************/
 
use "$files/rais/BR/rais_20102017.dta", clear
keep if year>=2012 & year<=2017
keep if emp1231==1
keep if admmonth==0
keep if gender==2

gegen workers = sum(emp1231), by(fakeid_estab year)
gen dayswork = 365*workers
gegen daysleave = sum(LOA_days), by(fakeid_estab year)
gen shdaysleave = daysleave/dayswork

forval i = 1/3 {
	gen start`i'_mdy = mdy(LOA_startmonth`i',LOA_startday`i',year)
	gen end`i'_mdy = mdy(LOA_endmonth`i',LOA_endday`i',year)
	gen LOA`i'_days = end`i'_mdy - start`i'_mdy + 1
}

forval i = 10(10)70 {
	gen LOA_days_`i' = 0
	replace LOA_days_`i' = LOA_days_`i' + LOA1_days if (LOA_cause1==`i')&(LOA1_days>0)&(LOA1_days<.) 
	replace LOA_days_`i' = LOA_days_`i' + LOA2_days if (LOA_cause2==`i')&(LOA2_days>0)&(LOA2_days<.) 
	replace LOA_days_`i' = LOA_days_`i' + LOA3_days if (LOA_cause3==`i')&(LOA3_days>0)&(LOA3_days<.) 
	replace LOA_days_`i' = 365 if (LOA_days_`i'>365)&(LOA_days_`i'<.)
}

forval i = 10(10)70 {
	gegen daysleave_`i' = sum(LOA_days_`i'), by(fakeid_estab year)
	gen shdaysleave_`i' = daysleave_`i'/dayswork
}

gegen xxx = tag(fakeid_estab year)
keep if xxx==1
keep fakeid_estab year shdaysleave*
xtset fakeid_estab year
rename (shdaysleave shdaysleave_10 shdaysleave_20 shdaysleave_30 shdaysleave_40 shdaysleave_50 shdaysleave_60 shdaysleave_70) ///
	(shdaysleavefem shdaysleavefem_10 shdaysleavefem_20 shdaysleavefem_30 shdaysleavefem_40 shdaysleavefem_50 shdaysleavefem_60 shdaysleavefem_70)
compress
save "$files/shdaysleavefem.dta", replace
 
 
cap log close		
